import Web3 from 'web3'
import { ethUrl, contractAbi, CONTRACT_ADDRESS } from './contractConfig'

window.Web3 = Web3

const GlobalWeb3 = window.web3 && typeof window.web3.sendTransaction === 'function'
export function auth() {
  GlobalWeb3.personal
}

export function formatAddr(addr = '') {
  const len = addr.length
  return `${addr.substring(0, 6)}...${addr.substring(len - 4, len)}`
}

export function addAttr(a, b, attr, newAttr) {
  const aattr = a[attr] || 0
  const battr = b[attr] || 0
  return aattr + battr
}

function getInstance() {
  if (window.ContractInstance) return window.ContractInstance
  console.log('web3Utils.js.vue==>>getInstance')
  const provider = new Web3.providers.HttpProvider(ethUrl)
  const nWeb3 = new Web3(provider)
  const contract = new nWeb3.eth.Contract(contractAbi, CONTRACT_ADDRESS)
  console.log('web3Utils.js.vue==>>getInstance, nWeb3: %o, contract: %o', nWeb3, contract)
  // const instance = contract.at(window.CONTRACT_ADDRESS)
  window.ContractInstance = contract
  return contract
}

export function enable() {
  return window.ethereum && window.ethereum.enable()
}

export async function getSpotPrice(_marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>getSpotPrice, marketIdx: %o', _marketIdx)
  const call = getInstance().methods.getSpotPrice(_marketIdx).call()
  return call
}

export async function getAccount(_trader, _marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>getAccount, marketIdx: %o, trader: %o', _marketIdx, _trader)
  const call = getInstance().methods.getAccount(_marketIdx, _trader).call()
  return call
}

export async function getPositionMargin(_trader, _marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>getPositionMargin, marketIdx: %o, trader: %o', _marketIdx, _trader)
  const call = getInstance().methods.getPositionMargin(_marketIdx, _trader).call()
  return call
}

export async function getTotalMargin(_trader, _marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>getTotalMargin, marketIdx: %o, trader: %o', _marketIdx, _trader)
  const call = getInstance().methods.getTotalMargin(_marketIdx, _trader).call()
  return call
}

export async function getUnrealizedPnl(_trader, _marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>getUnrealizedPnl, marketIdx: %o, trader: %o', _marketIdx, _trader)
  const call = getInstance().methods.getUnrealizedPnl(_marketIdx, _trader).call()
  return call
}

export async function markets(_marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>markets, marketIdx: %o', _marketIdx)
  const call = getInstance().methods.markets(_marketIdx).call()
  return call
}

export async function getMarketSize(_marketIdx = window._marketIdx, _side) {
  console.log('web3Utils.js.vue==>>getMarketSize, marketIdx: %o', _marketIdx)
  const call = getInstance().methods.getMarketSize(_marketIdx, _side).call()
  return call
}

export async function getMarketUnrealizedPnl(_marketIdx = window._marketIdx) {
  console.log('web3Utils.js.vue==>>getMarketUnrealizedPnl, marketIdx: %o', _marketIdx)
  const call = getInstance().methods.getMarketUnrealizedPnl(_marketIdx).call()
  return call
}

export async function getMaxOpenSize(_trader, _side, _price,_leverage, _marketIdx = window._marketIdx) {
  console.log(`web3Utils.js.vue==>>getMaxOpenSize.sliderMax, trader: %o, side: %o, price: %o, _leverage: %o,
                marketIdx: %o`, _trader, _side, _price, _leverage, _marketIdx)
  const call = getInstance().methods.getMaxOpenSize(_marketIdx, _trader, _side, _price, _leverage).call()
  return call
}



